import util from '../../utils/util'
import { WeElement, define } from 'omi'
import './index.css'
import {UploadSay,GetSay} from '../../api/api.js'
import { post, get, getUiAndLogin} from '../../utils/util.js'
import logo from './jingqu.png'
const app = getApp();
define('page-guest', class extends WeElement {
  config = {
    navigationBarTitleText: '客人说'
  }

  data = {
    xingx: [
      require('./xing.png'),
      require('./xing.png'),
      require('./xing.png'),
      require('./xing.png'),
    ],
    isSay:false,
    key_word:'',
    comments: '',
    page:'',
    all_page:''
  }

  install() {
    if (app.globalData.access_token!=''){
      this.data.isLogin=true;
      this.update();
    }
    get(GetSay,{
      page:1,
      page_num:15
    }).then((res=>{
      console.log(res)
      this.data.key_word = res.data.key_word;
      this.data.comments = res.data.say_list;
      this.data.page = res.data.page;
      this.data.all_page = res.data.all_page;
      this.update();
    }))
  }

  show_say=(e)=>{
    console.log('111',app.globalData)
    if(app.globalData.access_token!=''){
      this.data.isSay = !this.data.isSay;
      this.update();
      
    }else{
      getUiAndLogin(e.detail, app)
    }
  }

  say = (e) => {
    if(e.detail.value.textarea!=''){
      this.data.isSay = false;
      this.update();
      post(UploadSay, {
        content: e.detail.value.textarea
      })
    }else{
      wx.showToast({
        title: '请输入评论',
        icon: 'none',
        mask: true
      })
    }
  }

  tolast=(e)=>{
   if(this.data.page>1){
     get(GetSay, {
       page: Number(this.data.page) + 1,
       page_num: 15
     }).then((res => {
       console.log(res)
       this.data.key_word = res.data.key_word;
       this.data.comments = res.data.say_list;
       this.data.page = res.data.page;
       this.data.all_page = res.data.all_page;
       this.update();
     }))
   }
  }

  onReachBottom(){
    if(this.data.page<this.data.all_page){
      wx.showLoading({
        title: 'loading',
        mask: true
      })
      this.data.page = Number(this.data.page) + 1;
      this.update();
      get(GetSay, {
        page: this.data.page,
        page_num: 15
      }).then((res => {
        this.data.comments = this.data.comments.concat(res.data.say_list);
        this.data.page = res.data.page;
        this.data.all_page = res.data.all_page;
        this.update();
        wx.hideLoading();
      }))
    }
  }

  

  render() {
    const {isSay,key_word,comments,page,all_page} = this.data;
    return (
      <view class="f_color">
        <view class="top_but">
        <view class="marg">
          {
            key_word.map(item=>(
              <view class="tuij">{item.name}<text>{item.num}</text></view>
            ))
          }
        </view>
        </view>
        <view class="lists_box">
        {
          comments.map(itme=>(
            <view class="lis">
            <view class="img_box">
                  <image src={itme.member_avatar}> </image>
            </view>
            <view class="cen_cont">
                  <text>{itme.member_name}</text>
              {
                    xingx.map(itme=>(
                      <image src={itme} class="img"></image>
                    )
                    )
              }
            </view>
            <view class="right_time">
                  <text>{itme.time}</text>
            </view>
            <view class="butm_text">
                  <text>{itme.content}</text>
            </view>
          </view> 
        )
        )
        }
        
        </view>
        <button class="but_buttom" open-type="getUserInfo"  bindgetuserinfo={this.show_say}>
          <text>去评论</text>
        </button>
        {
          isSay&&(
            <view class='show_box'>
              <view class="mask" bindtap={this.show_say}></view>
              <view class='say_box'>
                <view class='say_title'>评价：</view>
                <form catchsubmit={this.say}>
                  <textarea maxlength='50' fixed='true' cursor-spacing='360rpx' name='textarea'></textarea>
                  <button form-type="submit">提交</button>
                </form>
              </view>
            </view>
          )
        }
      </view>
    )
  }
})